package 多线程.AQS;

public class TestMutex {
    private static int i = 0;
    private static Mutex mutex = new Mutex();
    //使用自定义的Mutex进行同步处理的a++
    public static void increase() {
        mutex.lock();
        i++;
        mutex.unlock();
    }
    public static void main(String[] args) throws Exception {
        //启动十个线程，每个线程累加10000次
        for (int j = 0; j < 10; j++) {
            new Thread(() -> {
                for (int k = 0; k < 10000; k++) {
                    increase();
                }
            }).start();
        }
        while (Thread.activeCount() > 2) {
            Thread.yield();
        }
        System.out.println(i);//100000
    }
}